﻿using PetaPoco.NetCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace BaseSimpleDao
{
    /// <summary>
    /// 基础数据库业务类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseService<T>
        where T : BaseModel
    {
        public virtual BaseProvider<T> Provider { get { return new BaseProvider<T>(); } }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns> 
        public virtual bool Add(T t)
        {
            return Provider.Add(t);
        }

        /// <summary>
        /// 添加列表对象
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public virtual int AddList(List<T> list)
        {
            return Provider.AddList(list);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual int Remove(T t)
        {
            return Provider.Remove(t);
        }

        /// <summary>
        /// 根据筛选器删除对象
        /// </summary>
        /// <param name="filters"></param>
        /// <returns></returns>
        public virtual int RemoveByFilter(IEnumerable<BaseFilter> filters)
        {
            return Provider.Remove(filters);
        }

        /// <summary>
        /// 根据数据对象
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public virtual int Update(T t)
        {
            return Provider.Update(t);
        }

        /// <summary>
        /// 根据Id删除对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual int RemoveById(long id)
        {
            return Provider.RemoveById(id);
        }

        /// <summary>
        /// 更新列表对象
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public virtual int UpdateList(IEnumerable<T> list)
        {
            return Provider.UpdateList(list);
        }

        /// <summary>
        /// 根据简单筛选器获取对象Id
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long GetItemIdBySingleFilter(string name, object value)
        {
            return Provider.GetItemIdByFilter(name, value);
        }

        /// <summary>
        /// 根据简单筛选器获取对象
        /// </summary>
        /// <param name="name">列名</param>
        /// <param name="value">列值</param>
        /// <returns></returns>
        public T GetItemBySingleFilter(string name, object value)
        {
            return Provider.GetItemByFilter(name, value);
        }

        /// <summary>
        /// 获取全部对象
        /// </summary>
        /// <param name="orders"></param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetAll(IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetAll(orders);
        }

        /// <summary>
        /// 获取前N条数据
        /// </summary>
        /// <param name="count">数据限制</param>
        /// <param name="filters">筛选器</param>
        /// <param name="orders">排序依据</param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetTop(int count, IEnumerable<BaseFilter> filters = null, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetTop(count, filters, orders);
        }

        /// <summary>
        /// 根据Id获取列表对象
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetListByIds(IEnumerable<long> ids, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetListByIds(ids, orders);
        }

        /// <summary>
        /// 根据筛选对象获取列表
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetListByFilter(IEnumerable<BaseFilter> filters, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetListByFilter(filters, orders);
        }

        /// <summary>
        /// 根据简单筛选器获取列表数据
        /// </summary>
        /// <param name="name">列名</param>
        /// <param name="value">列值</param>
        /// <param name="orders">排序依据</param>
        /// <returns></returns>
        public virtual IEnumerable<T> GetListBySingleFilter(string name, object value, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetListBySingleFilter(name, value, orders);
        }

        /// <summary>
        /// 获取第一个或者默认的对象
        /// </summary>
        /// <param name="filters">筛选器</param>
        /// <param name="orders">排序依据</param>
        /// <returns></returns>
        public virtual T GetFirstOrDefault(IEnumerable<BaseFilter> filters, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetFirstOrDefault(filters, orders);
        }

        /// <summary>
        /// 根据Id获取具体数据对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T GetItemById(long id)
        {
            return Provider.GetItemById(id);
        }

        /// <summary>
        /// 根据筛选器获取数据数量
        /// </summary>
        /// <param name="filters">筛选器</param>
        /// <returns></returns>
        public virtual int GetCount(IEnumerable<BaseFilter> filters = null)
        {
            return Provider.GetCount(filters);
        }

        /// <summary>
        /// 根据筛选器获取数据Id列表
        /// </summary>
        /// <param name="filters">筛选器</param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public virtual IEnumerable<long> GetItemIdList(IEnumerable<BaseFilter> filters = null, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetItemIdList(filters, orders);
        }

        /// <summary>
        /// 获取前N条数据的Id
        /// </summary>
        /// <param name="count">数量限制</param>
        /// <param name="filters">过滤器</param>
        /// <param name="orders">排序依据</param>
        /// <returns></returns>
        public virtual IEnumerable<long> GetTopIdList(int count, IEnumerable<BaseFilter> filters = null, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetTopIdList(count, filters, orders);
        }

        /// <summary>
        /// 获取第一个或者默认的数据对象Id
        /// </summary>
        /// <param name="filters"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public virtual long GetFirstOrDefaultId(IEnumerable<BaseFilter> filters = null, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetFirstOrDefaultId(filters, orders);
        }

        /// <summary>
        /// 获取分页数据列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="itemsPerPage">每页数量</param>
        /// <param name="filters">过滤器</param>
        /// <param name="orders">排序依据</param>
        /// <returns></returns>
        public virtual Page<T> GetPage(int page, int itemsPerPage, IEnumerable<BaseFilter> filters, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetPage(page, itemsPerPage, filters, orders);
        }

        /// <summary>
        /// 根据id获取所在分页数
        /// </summary>
        /// <param name="id"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="filters"></param>
        /// <param name="orders"></param>
        /// <returns></returns>
        public virtual int GetPageIndex(long id, int itemsPerPage, IEnumerable<BaseFilter> filters, IEnumerable<BaseOrder> orders = null)
        {
            return Provider.GetPageIndex(id, itemsPerPage, filters, orders);
        }
    }
}
